package com.smmisia.auth.service.impl;

import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.lang.Assert;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.smmisia.common.entity.auth.SysMenu;
import com.smmisia.common.entity.auth.SysRoleMenu;
import com.smmisia.common.entity.auth.User;
import com.smmisia.common.entity.auth.convertor.SysMenuConvertor;
import com.smmisia.common.enums.DeleteEnum;
import com.smmisia.common.enums.StatusEnum;
import com.smmisia.common.request.auth.MenuRequest;
import com.smmisia.auth.mapper.SysMenuMapper;
import com.smmisia.auth.mapper.SysRoleMenuMapper;
import com.smmisia.auth.mapper.UserMapper;
import com.smmisia.auth.service.SysMenuService;
import com.smmisia.common.Result.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Optional;

/**
 * 菜单业务实现类
 *
 * @author smmisia
 * @date 2024/7/27
 */
@Service
@Slf4j
public class SysMenuServiceImpl extends ServiceImpl<SysMenuMapper, SysMenu>
        implements SysMenuService {

    @Autowired
    private SysMenuMapper sysMenuMapper;

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private SysRoleMenuMapper sysRoleMenuMapper;

    @Override
    public Result<String> addMenu(MenuRequest menuRequest) {
        SysMenu menu = SysMenuConvertor.INSTANCE.toEntity(menuRequest);
        LambdaQueryWrapper<SysMenu> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(SysMenu::getMenuName, menu.getMenuName());
        SysMenu sysMenu = sysMenuMapper.selectOne(wrapper);
        if (sysMenu != null) {
            return Result.fail("菜单名不能重复");
        }
        Integer loginId = (Integer) StpUtil.getLoginId();
        User user = userMapper.selectById(loginId);
        String username = user.getUsername();
        menu.setVisible(String.valueOf(DeleteEnum.UN_DELETE.getCode()));
        menu.setStatus(StatusEnum.NORMAL.getDesc());
        menu.setCreateBy(username);
        menu.setCreateTime(LocalDateTime.now());
        int result = sysMenuMapper.insert(menu);
        Assert.isTrue(result == 1, () -> new RuntimeException("添加失败"));
        return Result.ok("添加成功");
    }

    @Override
    public Result<String> deleteMenu(Integer id) {
        LambdaQueryWrapper<SysRoleMenu> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(SysRoleMenu::getMenuId, id);
        SysRoleMenu sysRoleMenu = sysRoleMenuMapper.selectOne(wrapper);
        if (sysRoleMenu != null) {
            return Result.fail("该菜单已被角色使用，无法删除");
        }
        int result = sysMenuMapper.deleteById(id);
        Assert.isTrue(result == 1, () -> new RuntimeException("删除失败"));
        return Result.ok("删除成功");
    }

    @Override
    public Result<String> updateMenu(MenuRequest menuRequest) {
        Integer loginId = (Integer) StpUtil.getLoginId();
        User user = userMapper.selectById(loginId);
        String username = user.getUsername();
        LambdaQueryWrapper<SysMenu> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(SysMenu::getMenuName, menuRequest.getMenuName());
        SysMenu menu = sysMenuMapper.selectOne(wrapper);
        if (menu != null) {
            return Result.fail("菜单名不能重复");
        }
        SysMenu convert = SysMenuConvertor.INSTANCE.toEntity(menuRequest);
        convert.setUpdateTime(LocalDateTime.now());
        convert.setUpdateBy(username);
        int result = sysMenuMapper.updateById(convert);
        Assert.isTrue(result == 1, () -> new RuntimeException("修改失败"));
        return Result.ok("修改成功");
    }

    @Override
    public Result<List<SysMenu>> getMenu(Integer pageNum, Integer pageSize) {
        IPage<SysMenu> page = new Page<SysMenu>(pageNum, pageSize);
        sysMenuMapper.selectPage(page, null);
        List<SysMenu> sysMenus = sysMenuMapper.selectPage(page, null).getRecords();
        return Result.ok(sysMenus);
    }

    @Override
    public Result<SysMenu> getMenuInfo(Integer id) {
        return Optional.ofNullable(sysMenuMapper.selectById(id))
                .map(Result::ok)
                .orElseGet(() -> Result.fail("菜单信息不存在"));
    }


}




